package com.artron.mediaartron.ui.fragment.factory;

import android.support.v4.app.Fragment;
import android.support.v4.util.SparseArrayCompat;

import com.artron.baselib.base.BaseFragment;
import com.artron.baselib.base.BaseLoadingFragment;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.fragment.home.CustomMadeFragment;
import com.artron.mediaartron.ui.fragment.home.MyCenterFragment;
import com.artron.mediaartron.ui.fragment.home.ScanFragment;
import com.artron.mediaartron.ui.fragment.login.LoginWrapperFragment;
import com.artron.mediaartron.ui.fragment.shoppingcart.ShoppingCartFragment;


/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-07-17.
 */

public class HomeFragmentFactory {

    public static final int CUSTOM_MADE = 0;//定制
    public static final int SCAN = 4;//扫一扫
    public static final int SHOPPING_CART = 1;//购物车
    public static final int MY_CENTER = 2;//我的

    private static HomeFragmentFactory instance = new HomeFragmentFactory();

    private HomeFragmentFactory() {
    }

    public static HomeFragmentFactory getInstance() {
        return instance;
    }

    private SparseArrayCompat<BaseFragment> factory = new SparseArrayCompat<>();

    public BaseFragment getFragment(int position) {
        BaseFragment fragment = null;
        if (factory != null) {
            fragment = factory.get(position);
        }

        if (fragment == null) {
            switch (position) {
                case CUSTOM_MADE:
                    fragment = CustomMadeFragment.newInstance();
                    break;
                case SCAN:
                    fragment = ScanFragment.newInstance();
                    break;
                case SHOPPING_CART:
                    fragment = LoginWrapperFragment.newInstance(ShoppingCartFragment.newInstance());
                    break;
                case MY_CENTER:
                    fragment = MyCenterFragment.newInstance();
                    break;
            }
        }

        if (fragment != null && factory.indexOfValue(fragment) == -1) {
            factory.put(position, fragment);
        }

        return fragment;
    }

    public String getTitle(int position) {
        switch (position) {
            case CUSTOM_MADE:
                return UIUtils.getString(R.string.custom_made);
            case SCAN:
                return UIUtils.getString(R.string.scan);
            case SHOPPING_CART:
                return UIUtils.getString(R.string.shopping_cart);
            case MY_CENTER:
                return UIUtils.getString(R.string.my_center);
        }
        return "";
    }

    public void updateShoppingCart() {
        BaseFragment fragment = this.getFragment(HomeFragmentFactory.SHOPPING_CART);
        if (fragment instanceof LoginWrapperFragment) {
            BaseFragment contentFragment = ((LoginWrapperFragment) fragment).getContentFragment();
            if (contentFragment instanceof BaseLoadingFragment) {
                ((BaseLoadingFragment) contentFragment).showImmediate();
            }
        }
    }

    public Fragment getMainFragment() {
        BaseFragment fragment = getFragment(CUSTOM_MADE);
        return fragment.getParentFragment();
    }
}
